package com.china_goldcard.paydialog;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

/**
 * Created by jk on 16/11/2.
 */

public class PayOrderAdapter extends RecyclerView.Adapter<PayOrderViewHolder> {

    private List<PayOrderModel> data;

    private Context context;

    private OnItemClickListener onItemClickListener;

    public interface OnItemClickListener{
        void onItemClick(int position,PayOrderModel model);
    }

    public PayOrderAdapter(List<PayOrderModel> data, Context context) {
        this.data = data;
        this.context = context;
    }

    @Override
    public PayOrderViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(context).inflate(R.layout.pay_dialog_order_item_view,parent,false);
        return new PayOrderViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(PayOrderViewHolder holder, final int position) {
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener!=null){
                    onItemClickListener.onItemClick(position,data.get(position));
                }
            }
        });
        holder.labelTv.setText(data.get(position).getType().getKey());
        holder.valueTv.setText(data.get(position).getOrderItemValue());
        switch (data.get(position).getType()){
            case PayWayInfo:
                holder.arrowRightIcon.setVisibility(View.VISIBLE);
                break;
            default:
                holder.arrowRightIcon.setVisibility(View.GONE);
                break;
        }
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public void reloadData(List<PayOrderModel> data){
        this.data = data;
        notifyDataSetChanged();
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
}

class PayOrderViewHolder extends RecyclerView.ViewHolder{

    TextView labelTv;

    TextView valueTv;

    ImageView arrowRightIcon;

    public PayOrderViewHolder(View itemView) {
        super(itemView);
        labelTv = (TextView) itemView.findViewById(R.id.pay_order_label);
        valueTv = (TextView) itemView.findViewById(R.id.pay_order_value);
        arrowRightIcon = (ImageView) itemView.findViewById(R.id.pay_order_right_arrow_icon);
    }
}